package com.cmcc.aregister.ui.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.opengl.Visibility;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;

import com.cmcc.aregister.R;
import com.cmcc.aregister.cs.constant.ConfigUtilHelper;

/**
 * 本地网页加载
 * @author ld 20130325
 * 
 */
public class NoticeWebView extends ParentActivity
{
    private WebView webview;
    private ProgressDialog progressDialog;
    Thread thread;
    String url;
    private Button backButton;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notice_webview);
        ((ImageView) findViewById(R.id.title))
            .setBackgroundResource(R.drawable.notice);
        webview = (WebView) findViewById(R.id.website_view);
        backButton = (Button) findViewById(R.id.top_left_image);
        backButton.setVisibility(View.VISIBLE);
        backButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v)
            {
                back();
            }
        });
        url = getIntent().getExtras().getString("url");
        loadUrl();
    }

    private void loadUrl()
    {
        progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setTitle(getString(R.string.notice));
        progressDialog.setIcon(android.R.drawable.ic_dialog_info);
        progressDialog.setMessage(getString(R.string.loading_web));
        progressDialog.setIndeterminate(true);
        progressDialog.setCancelable(true);
        progressDialog.show();

        thread = new Thread(new Runnable() {
            @Override
            public void run()
            {
                webview.getSettings().setJavaScriptEnabled(true);
                webview.getSettings().setPluginsEnabled(true);
                webview.getSettings().setSupportZoom(true);
                // webview.getSettings().setBuiltInZoomControls(true);
                // 加载url前，设置图片阻塞
                // webview.getSettings().setBlockNetworkImage( true );
                webview.loadUrl(url);
                MyWebViewClient myWebView = new MyWebViewClient();
                webview.setWebViewClient(myWebView);
            }
        });
        thread.start();

    }

    @Override
    // 设置回退
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview != null
            && webview.canGoBack())
        {
            back();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void back()
    {
        Intent intent = new Intent(this, NewIndexActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        this.startActivity(intent);
        finish();
    }

    private class MyWebViewClient extends WebViewClient
    {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url)
        {
            // 加载完毕后，关闭图片阻塞
            webview.getSettings().setBlockNetworkImage(false);
            progressDialog.dismiss();
        }

        @Override
        public void onReceivedError(WebView view, int errorCode,
            String description, String failingUrl)
        {
            // TODO Auto-generated method stub
            super.onReceivedError(view, errorCode, description, failingUrl);
            progressDialog.dismiss();
        }
    }

    @Override
    protected void onDestroy()
    {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

    @Override
    protected void onPause()
    {
        // TODO Auto-generated method stub
        super.onPause();
    }

    @Override
    protected void onRestart()
    {
        // TODO Auto-generated method stub
        super.onRestart();
    }

    @Override
    protected void onResume()
    {
        // TODO Auto-generated method stub
        super.onResume();
    }

    @Override
    protected void onStart()
    {
        // TODO Auto-generated method stub
        super.onStart();
    }

    @Override
    protected void onStop()
    {
        // TODO Auto-generated method stub
        super.onStop();
    }
}
